﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Pantry.DataAccess;
using Pantry.Business;
using Pantry.Commons;
using Pantry.Entity;

namespace Pantry.UnitTest
{
    [TestFixture]
    public class TestProgramDao
    {
        private ProgramDao programDao;
        private const string ConnectionString = "Server=10.16.102.126;Database=Pantry_MockProject;Integrated Security=True;";

        [SetUp]
        public void Init()
        {
            programDao = new ProgramDao(new SqlConnection(ConnectionString));
        }

        [Test]
        public void TestInsertProgram_DuplicateProgramName()
        {
            var program = new Program
            {
                ProgramName = "Duplicate program name",
                Description = "Do con bo",
                IsActive = true,
                ContactId = 1,
                OrganizationId = 1
            };
            const int expected = -1;
            var actual = programDao.Insert(program);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestInsertProgram_success()
        {
            var program = new Program
            {
                ProgramId = 2,
                ProgramName = "Program insert success",
                Description = "Do con bo",
                IsActive = true,
                ContactId = 1,
                OrganizationId = 1
            };
            const int expected = 1;
            var actual = programDao.Insert(program);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestUpdateProgram_DuplicateProgramName()
        {
            var program = new Program
            {
                ProgramId = 2,
                ProgramName = "Duplicate program name",
                Description = "Do con bo",
                IsActive = true,
                ContactId = 1,
                OrganizationId = 1
            };
            const int expected = -1;
            var actual = programDao.Update(program);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TestUpdateProgram_success() 
        {
            var  program = new Program
            {
                ProgramId = 2,
                ProgramName = "Heo Map",
                Description = "Do con bo",
                IsActive = true,
                ContactId = 1,
                OrganizationId = 1
            };
            var result = programDao.Update(program);
            var programActual = (Program)programDao.RetrieveEntity(program.ProgramId);
            Assert.AreEqual(1, result);
            Assert.AreEqual(program.ProgramName, programActual.ProgramName);
            Assert.AreEqual(program.Description, programActual.Description);
            Assert.AreEqual(program.IsActive, programActual.IsActive);
            Assert.AreEqual(program.ContactId, programActual.ContactId);
            Assert.AreEqual(program.OrganizationId, programActual.OrganizationId); 
        }

        [Test]
        public void TestChangeActiveProgram()
        {
            var program = new Program
            {
                ProgramId = 1,
                IsActive = true
            };
            const int expected = 1;
            var actual = programDao.ChangeIsActive("Program","IsActive","True","ProgramId",1);
            var programExpected = (Program)programDao.RetrieveEntity(program.ProgramId);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(program.IsActive, programExpected.IsActive);
        }
    }
}
